package plus.lgx.proj1

trait Logger {

  def log(message: String): Unit = {
    println("将消息记录到日志中。消息：" + message)
  }

}

case class Calculator() extends Logger {

  def add(x: Int , y: Int): Int = {
    val ret = x + y
    log(s"执行 add 操作，参数为：($x, $y)，结果为 $ret。")
    ret
  }

  def subtract(x: Int , y: Int): Int = {
    val ret = x - y
    log(s"执行 subtract 操作，参数为：($x, $y)，结果为 $ret。")
    ret
  }

  def calculate(x: Int, y: Int, oper: Char): Any = {
    oper match {
      case '+' => x + y
      case '-' => x - y
      case '*' => x * y
      case '/' => x / y
      case '%' => x % y
      case _ => "Invalid operator"
    }
  }

}

object Calculator {

  def main(args: Array[String]): Unit = {
    val calc = Calculator()
    calc.add(1, 2)
    calc.subtract(5, 1)

    println(calc.calculate(23, 45, '+'))
    println(calc.calculate(25, 56, '-'))
    println(calc.calculate(64, 21, '*'))
    println(calc.calculate(32, 65, '/'))
    println(calc.calculate(47, 5, '%'))
    println(calc.calculate(47, 50, '?'))
  }

}
